package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.com.lyj6851.modules.order.domain.YdyExpress;
import cn.com.lyj6851.modules.order.service.YdyExpressService;
import cn.com.lyj6851.modules.order.service.dto.YdyExpressQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
*/
@Api(tags = "商城:快递管理")
@RestController
@RequestMapping("api")
public class ExpressController {


    private final YdyExpressService yxExpressService;

    public ExpressController(YdyExpressService yxExpressService) {
        this.yxExpressService = yxExpressService;
    }

    @AopLog("查询快递")
    @ApiOperation(value = "查询快递")
    @GetMapping(value = "/ydyExpress")
    @PreAuthorize("hasAnyRole('admin','EXPRESS_ALL','EXPRESS_SELECT')")
    public ResponseEntity getYdyExpresss(YdyExpressQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxExpressService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ForbidSubmit
    @AopLog("新增快递")
    @ApiOperation(value = "新增快递")
    @PostMapping(value = "/ydyExpress")
    @PreAuthorize("hasAnyRole('admin','EXPRESS_ALL','EXPRESS_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YdyExpress resources){
        return new ResponseEntity<>(yxExpressService.save(resources),HttpStatus.CREATED);
    }

    @ForbidSubmit
    @AopLog("修改快递")
    @ApiOperation(value = "修改快递")
    @PutMapping(value = "/ydyExpress")
    @PreAuthorize("hasAnyRole('admin','EXPRESS_ALL','EXPRESS_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdyExpress resources){
        yxExpressService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除快递")
    @ApiOperation(value = "删除快递")
    @DeleteMapping(value = "/ydyExpress/{id}")
    @PreAuthorize("hasAnyRole('admin','EXPRESS_ALL','EXPRESS_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        yxExpressService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
